package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.YddAdvimgDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.service.IYddAdvimgService;
import com.itheima.ydd.service.IYddAdvposService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Slf4j
@RestController
@RequestMapping("/ydd_advimg")
public class YddAdvimgController {
    @Autowired
    private IYddAdvposService yddAdvposService;
    @Autowired
    private IYddAdvimgService yddAdvimgService;

    /**
     * 分页查询
     * @param advimgdesc
     * @param advimgpos
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData list(String advimgdesc ,String advimgpos ,int page ,int limit){
      log.info("查询参数{}",advimgdesc,advimgpos);

      //构建分页构造器对象
        Page<YddAdvimg> pageInfo = new Page<>(page, limit);
        Page<YddAdvimgDto> pageDto = new Page<>();
        //条件构造器
        LambdaQueryWrapper<YddAdvimg> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(advimgdesc),YddAdvimg::getAdvimgdesc,advimgdesc);
        queryWrapper.like(StringUtils.isNotEmpty(advimgpos),YddAdvimg::getAdvimgpos,advimgpos);
        //执行分页查询
        yddAdvimgService.page(pageInfo,queryWrapper);

        List<YddAdvimg> records = pageInfo.getRecords();
        List<YddAdvimgDto> list = records.stream().map((item) -> {
            YddAdvimgDto dto = new YddAdvimgDto();
            BeanUtils.copyProperties(item,dto);
            LambdaQueryWrapper<YddAdvpos> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(item.getId() != null,YddAdvpos::getId,item.getAdvimgpos());
            YddAdvpos advpos = yddAdvposService.getOne(wrapper);
            if (advpos != null){
                dto.setAdvposname(advpos.getAdvposname());
            }
            return dto;
        }).collect(Collectors.toList());
        pageDto.setRecords(list);

        HashMap<String, Object> map = new HashMap<>();
        map.put("count",pageInfo.getTotal());
        map.put("totalPages",pageInfo.getPages());
        map.put("pageSize",pageInfo.getSize());
        map.put("currentPage",pageInfo.getCurrent());
        map.put("data",pageDto.getRecords());
        return ResultData.ok("",map);

    }


    //修改广告图数据回显
    @GetMapping("/rowInfo")
    public ResultData rowInfo(Integer id) {
        YddAdvimg advimg = yddAdvimgService.getById(id);
        if (advimg != null){
            return ResultData.ok("",advimg);
        }else {
            return ResultData.error("没有信息啊吊毛!");
        }
    }

    //修改广告图信息
    @PostMapping("/update")
    public R update(@RequestBody YddAdvimg yddAdvimg) {

        if (yddAdvimg != null){
            yddAdvimgService.updateById(yddAdvimg);
            return R.success("", "修改成功靓仔~");
        }else {
            return R.error("这是空的啊，吊毛!");
        }

    }

    //添加广告图
    @PostMapping("/add")
    public R add(@RequestBody YddAdvimg yddAdvimg) {
        log.info("接收参数为: {}",yddAdvimg);
        boolean save = yddAdvimgService.save(yddAdvimg);
        if (save) {
            return R.success("添加成功靓仔~", "");
        } else {
            return R.error("添加失败吊毛!");
        }
    }


    //删除广告图
    @GetMapping("/del")
    public R delete(Integer id) {
        log.info("删除的id为: {}",id);
        YddAdvimg yddAdvimg = yddAdvimgService.getById(id);
        String defaultData = yddAdvimg.getDefaultData();
        if (defaultData != null && defaultData.equals("1")){
            return R.error("系统默认数据，不能删啊吊毛!");
        }else {
            yddAdvimgService.removeById(id);
            return R.success("删除成功了靓仔~", "");
        }

    }
}
